package com.dys.controller;

import com.dys.dto.UserDTO;
import com.dys.pojo.Role;
import com.dys.pojo.User;
import com.dys.service.IRoleService;
import com.dys.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @className: com.dys.controller.UserController
 * @description: 用户管理
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/14 21:28
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @GetMapping(value = "/selectUsers")
    // @ResponseBody
    public String selectUsers(User user, Model model) throws Exception {
        List<User> list = userService.listUserInfoByUser(user);
        model.addAttribute("list", list);
        return "user/user";
    }

    /**
     * 处理跳转
     */
    @RequestMapping(value = "/userDispatch")
    public String handleUserDispatch(Integer userId, Model model) throws Exception {
        if (userId != null && userId > 0) {
            // 更新用户数据,根据用户编号查询用户资料
            User user = userService.findUserInfoByUserId(userId);
            model.addAttribute("user", user);
            // 查询当前用户具有的角色
            List<Integer> userRoleIds = userService.findUserRoleIdsByUserId(userId);
            model.addAttribute("userRoleIds", userRoleIds);
        }
        // 获取所有角色信息
        List<Role> list = roleService.listRoleInfoByRole(new Role());
        model.addAttribute("roles", list);
        return "user/updateUser";
    }

    @RequestMapping(value = "/saveUserInfo")
    public String saveUserInfo(UserDTO userDTO) throws Exception {
        // 1.保存用户信息
        int count = userService.saveUserInfo(userDTO);
        // 2.保存用户和角色的关联信息
        return "redirect:/user/selectUsers";
    }

    @RequestMapping(value = "/checkUsername")
    @ResponseBody
    public String checkUsername(User user) throws Exception {
        List<User> list = userService.listUserInfoByUser(user);
        if (list == null || list.size() == 0) {
            // 账号存在: 数据库中无数据
            return "1";
        } else {
            return "0";
        }
    }

    @RequestMapping(value = "/deleteUserByUserId")
    public String deleteUserByUserId(int userId) throws Exception {
        int count = userService.deleteUserInfoByUserId(userId);
        return "redirect:/user/selectUsers";
    }

    @RequestMapping(value = "/findUserInfoByPage")
    public String findUserInfoByPage(Model model, UserDTO userDTO) throws Exception {
        PageInfo<User> userInfoByPage = userService.findUserInfoByPage(userDTO);
        model.addAttribute("pageModel", userInfoByPage);
        return "user/user";
    }
}
